export async function open_new_tab(
  url: string,
  newWindow: boolean,
  windowId?: number
): Promise<chrome.tabs.Tab> {
  let tabId;
  if (newWindow) {
    let window = await chrome.windows.create({
      type: 'normal',
      state: 'maximized',
      url: url,
    } as any as chrome.windows.CreateData);
    windowId = window.id as number;
    let tabs = window.tabs || [
      await chrome.tabs.create({
        url: url,
        windowId: windowId,
      }),
    ];
    tabId = tabs[0].id as number;
  } else {
    if (!windowId) {
      const window = await chrome.windows.getCurrent();
      windowId = window.id;
    }
    let tab = await chrome.tabs.create({
      url: url,
      windowId: windowId,
    });
    tabId = tab.id as number;
  }
  let tab = await waitForTabComplete(tabId);
  await sleep(200);
  return tab;
}

